<template>
	<div class="page">
		<a-row>
		  <a-col :span="6">
			  <a-form-item label="站号" >
				  <a-select size="large" v-model:value="station" style="width: 100px;">
					  <a-select-option :value="i" v-for="i in 255" :key="i">{{ i }}</a-select-option>
				  </a-select>
			  </a-form-item>
		  </a-col>
		  <a-col :span="6">
			  <a-form-item label="输入数量" >
				  <a-input-number @change="inputNumChange" type="number" style="width: 80px;" size="large" v-model:value="inputNum" />
			  </a-form-item>
		  </a-col>
		  <a-col :span="6">
			  <a-form-item label="输出数量" >
				  <a-input-number @change="outputNumChange" style="width: 80px;" size="large" v-model:value="outputNum" />
			  </a-form-item>
		  </a-col>
		  <a-col :span="6">
			<a-checkbox v-model:checked="autoread">自动读取</a-checkbox>	
		  </a-col>
		</a-row>


		<a-row type="flex" :gutter="16" style="margin-top: 20px;">
			<a-col :span="16">
				<a-card title="输出操作，输出状态不自动读取">
					<div>
						<a-switch v-model:checked="checked">
							<template #checkedChildren>
								开
							</template>
							<template #unCheckedChildren>
								关
							</template>
						</a-switch>
					</div>
				</a-card>
				<a-card title="输出操作，输出状态不自动读取">

				</a-card>
			</a-col>

			<a-col :span="8">

			</a-col>
		</a-row>


	</div>
</template>

<script>
import { ipc } from '@/utils/ipcRenderer'
import { bytesToHexString, crc16Hex, divideToFloat32Hex, numberToHex, delay } from '@/utils/common'
// const turnOnCmd = '06 00 00 00 01'
// const turnOffCmd = '06 00 00 00 00'
export default {
	data() {
		return {
			// 站号
			station: 3,
			inputNum: 1,
			outputNum: 1,
			autoread: true,
			result: [],
		};
	},
	mounted() {

	},
	methods: {
		// 添加CRC校验
		crc (hexString) {
            let cmd = hexString.replace(/\s/g, '');
            let crc = bytesToHexString(crc16Hex(cmd));
            return cmd + crc 
        },
		on(channel) {
			// 通道号0开始
			let channelHex = numberToHex(channel)
			let turnOnCmd = '06 00' + channelHex + '00 01'
			let address = numberToHex(this.station)
			let cmd = this.crc(address + turnOnCmd)
		},
		off(channel) {
			// 通道号0开始
			let channelHex = numberToHex(channel)
			let turnOffCmd = '06 00' + channelHex + '00 00'
			let address = numberToHex(this.station)
			let cmd = this.crc(address + turnOffCmd)
		},
		// 发送自定义指令
        send (value) {
            // this.result.unshift({ event: 'send', data: value })    
            this.pushData(value, 'send')        
            return ipc.invoke('controller.modbusRTU.send', { value })
        },
		pushData (data, state) {
			this.result.unshift({ event: state, data: data })
		},
		inputNumChange(value) {
			// this.inputNum = value
		},
		outputNumChange(value) {
			// this.outputNum = value
		},

	},


};
</script>

<style scoped>
.ant-input{
	text-align: center;
}
.ant-select{
	text-align: center;
}
:deep(.ant-form-item-label > label){
	height: 40px;
	font-size: 16px;
}
.ant-checkbox-wrapper{
	font-size: 16px;
	line-height: 40px;
}
.ant-card{
	margin-bottom: 20px;
}
</style>
  